﻿using Google.Apis.Authentication;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Util;
using System;
using System.IO;
using System.Security.Cryptography.X509Certificates;

namespace PB138___projekt
{
    /// <summary>
    /// Represents authenticator that allows application authentication.
    /// </summary>
    class ApplicationAuth : IAuth
    {
        private const string SERVICE_ACCOUNT_EMAIL
            = "515108601197@developer.gserviceaccount.com";

        IAuthenticator authenticator;

        /// <summary>
        /// Gets authenticator.
        /// </summary>
        public IAuthenticator Authenticator
        {
            get { return authenticator; }
        }

        /// <summary>
        /// Gets type of authenticator.
        /// </summary>
        public AuthenticatorType AuthenticatorType
        {
            get { return AuthenticatorType.ApplicationAuthenticator; }
        }

        /// <summary>
        /// Constructor for creating instances of <c>ApplicationAuth</c>.
        /// </summary>
        /// <param name="pkcsFilePath">Path to PKCS ♯12 (.p12) file.</param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="pkcsFilePath"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If string <paramref name="pkcsFilePath"/> is empty or consists only
        /// of white-space characters.
        /// </exception>
        /// <exception cref="FileNotFoundException">
        /// If file located at <paramref name="pkcsFilePath"/> can not be found.
        /// </exception>
        public ApplicationAuth(string pkcsFilePath)
        {
            if (pkcsFilePath == null)
            {
                throw new ArgumentNullException("pkcsFilePath");
            }

            if (string.IsNullOrEmpty(pkcsFilePath)
                || string.IsNullOrWhiteSpace(pkcsFilePath))
            {
                throw new ArgumentException(
                    "Path is empty or consists only of white-space characters!",
                    "pkcsFilePath");
            }

            if (!File.Exists(pkcsFilePath))
            {
                throw new FileNotFoundException("File was not found!",
                                                "pkcsFilePath");
            }

            X509Certificate2 certificate = new X509Certificate2
                                                (
                                                    pkcsFilePath,
                                                    "notasecret",
                                                    X509KeyStorageFlags.Exportable
                                                );

            var provider
                = new AssertionFlowClient(GoogleAuthenticationServer.Description,
                                          certificate)
                {
                    ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
                    Scope = DriveService.Scopes.Drive.GetStringValue()
                };

            // create authenticator
            authenticator = new OAuth2Authenticator<AssertionFlowClient>
                                (
                                    provider,
                                    AssertionFlowClient.GetState
                                );
        }
    }
}